package repositories;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import books.Author;

/**
 */
public class AuthorRepository extends HibernateGenericDAO<Author> implements GenericRepository<Author> {

    private static final long serialVersionUID = 1L;

    @Override
    protected Class<Author> getDomainClass() {
        return Author.class;
    }

    @SuppressWarnings("unchecked")
    public List<Author> findAuthorByName(final String pattern) {
        return (List<Author>) this.getHibernateTemplate().execute(new HibernateCallback<Object>() {
            @Override
            public List<Author> doInHibernate(final Session session) throws HibernateException, SQLException {
                Criteria criteria = session.createCriteria(Author.class);
                criteria.add(Restrictions.like("name", "%" + pattern + "%"));
                return criteria.list();
            }
        });
    }

}
